package com.runa.monitor.platinterface.netservice.equipment;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.*;
import com.runa.monitor.platinterface.backwork.impl.RccTaskFunction;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.dto.DtuHDRequestData;
import com.runa.rccprotocol.dto.ConcentratorRequestDataProxy;
import com.runa.rccprotocol.dto.en.EnMeterRequestData;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import com.runa.rccprotocol.dto.runa.RunaDeviceRequestData;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author diandian
 * @Date 2021/11/1 14:37
 */
@Slf4j
public class EnDriver extends NetEqu<String> {

    public EnDriver(String serial, Integer timeout) {
        super(serial, timeout);
    }


    @Override
    public void readNodeEqu(List<Node> nodeList, TaskFunction function, EquData.EquDataType type) {
        if (!online()) {
            function.doApply(CommStatus.OffLine, nodeList);
            return;
        }
        switch (type) {
            case METER:
                readHouseMeter(nodeList, function);
                break;
            default:
        }
    }

//    public Node findMeter(String serial) {
//        for (Node meter : hMeters) {
//            if (serial.equals(meter.getSerial())) {
//                return meter;
//            }
//        }
//        return null;
//    }
//
//    @Override
//    public Valve findValve(String serial) {
//        return null;
//    }
//
//    @Override
//    public TempPanel findPanel(String serial) {
//        return null;
//    }
//
//    @Override
//    public PublicUnit findUnit(String serial) {
//        return null;
//    }


    private void readHouseMeter(List<Node> valves, TaskFunction function) {
        for (Node node : valves) {
            String model = node.getModel();
            log.error(JSON.toJSONString(model));
            if ("RC型".equals(model)) {
                RunaDeviceRequestData runaDeviceRequestData = new RunaDeviceRequestData(serial());
                runaDeviceRequestData.readHeatMeter(node.getSerial(), (byte) 0x20, new byte[]{0, 0x11, 0x11}, new byte[]{0x1f, (byte) 0x90});
                runaDeviceRequestData.setRemain(node);
                sendRequest(new ConcentratorRequestDataProxy(serial(), new DtuHDRequestData(serial(), runaDeviceRequestData)), function);
            } else {
                EnMeterRequestData requestData = new EnMeterRequestData(serial());
                Boolean initRes = initAndChoose(requestData, node.getSerial());
                if (initRes) {
                    requestData.readHouseMeter();
                    ConcentratorRequestDataProxy requestDataProxy =
                            new ConcentratorRequestDataProxy(serial(), new DtuHDRequestData(serial(), requestData));
                    requestDataProxy.setRemain(Collections.singletonList(node));
                    sendRequest(requestDataProxy, function);
                    destroy(requestData, function.getPriority()-1);
                }
            }
        }
    }

    private Boolean initAndChoose(EnMeterRequestData requestData, String serial) {
        RccTaskFunction.SyncFunction chooseFunc = new RccTaskFunction.SyncFunction(null, timeout(), 15);
        requestData.choseHouseMeter(serial);
        super.sendRequest(new ConcentratorRequestDataProxy(serial(), new DtuHDRequestData(serial, requestData)), chooseFunc);
        Boolean chooseRes = chooseFunc.waitResult();
        log.info("chooseRes-------" + chooseRes);
        return chooseRes;
    }

    private void destroy(EnMeterRequestData requestData, int priority) {
        RccTaskFunction.SyncFunction closeFunc = new RccTaskFunction.SyncFunction(priority);
        requestData.destroy();
        ConcentratorRequestDataProxy requestDataProxy =
                new ConcentratorRequestDataProxy(serial(), new DtuHDRequestData(serial(), requestData));
        super.sendRequest(requestDataProxy, closeFunc);
        closeFunc.waitResult();
    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {

    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void valveOnOffControl(List<Node> valveSerial, boolean ifLock, Integer opening, TaskFunction function) {

    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> valveSerial, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> valveSerial, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valveSerial, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valveSerial, Node meterSerial, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }

}
